<template>
  <div style="padding: 10px">
    <!--    表格区域-->
    <div style="margin: 10px 0">
      <!--    有啥子功能？注：sortable用于排序  type="success"绿色-->
      <el-button @click="add" >新增</el-button>
    </div>
<!--    增添新增按钮按钮，引入add方法，实现对用户的新增功能-->
    <div style="margin: 10px 0">
      <!--      搜索区-->
      搜索：
      <el-input v-model="search" placeholder="输入要查询内容"  style="width: 20%" clearable></el-input>
      <el-button style="margin-left: 20px" @click="load">搜索</el-button>
    </div>
<!--    通过element-plus引入搜索框，通过输入用户的用户名实现对用户的搜索功能-->

    <el-table :data="tableData"  style="width: 100%">
      <el-table-column prop="id" label="ID" sortable width="180"></el-table-column>
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="age" label="年龄" width="180"></el-table-column>
      <el-table-column prop="sex" label="性别" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column  label="权限">
        <template #default="scope">
          <scan v-if="scope.row.power === 1">管理员</scan>
          <scan v-if="scope.row.power === 2">普通用户</scan>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button  size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除本行信息？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog
          v-model="dialogVisible"
          title="Tips"
          width="30%"
      >
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" ></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" size="medium" style="width: 85%"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <div>
              <el-radio v-model="form.sex" label="男" size="medium">男</el-radio>
              <el-radio v-model="form.sex" label="女" size="medium">女</el-radio>
            </div>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" size="medium" style="width: 85%"></el-input>
          </el-form-item>
          <el-form-item label="申请权限">
            <el-input v-model="form.power" size="medium" style="width: 85%"></el-input>
          </el-form-item>
          (1为管理员权限;2为普通用户权限)
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false" size="medium">取消</el-button>
        <el-button  @click="save" size="medium">确定</el-button
        >
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>

import request from "../utils/request";

export default {
  name: 'Home',
  components: {
  },
  //表中数据
  data() {
    return {
      search:'',
      form:{},
      dialogVisible: false,
      currentPage4:1,
      pageSize:10,
      total1:0,
      tableData: [
      ],
    }
  },
  created() {
    this.load()
  },
//  定义函数：
  methods:{
    load(){
      request.get("/user", {
        params:{
          pageNum: this.currentPage4,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res =>{
        console.log(res)
        this.tableData = res.data.records
        this.total1 = res.data.total1
      })
    },
    add(){
      this.dialogVisible = true
      this.form={}
      //  清空表单

    },
    save(){
      if (this.form.id) {//更新
        request.put("/user", this.form).then(res => {
          console.log(res)
          if (res.code === '0')
          {
            this.$message(
                {
                  type:"success",
                  message:"更新成功"
                }
            )
          }
          else {
            this.$message(
                {
                  type:"error",
                  message:res.msg
                }
            )
          }
          this.load()
          this.dialogVisible = false
        })
      }
      else {//新增
        request.post("/user", this.form).then(res => {
          console.log(res)
          if (res.code === '0')
          {
            this.$message(
                {
                  type:"success",
                  message:"新增成功"
                }
            )
          }
          else {
            this.$message(
                {
                  type:"error",
                  message:res.msg
                }
            )
          }
          this.load()
          this.dialogVisible = false
        })
      }
    },
    handleEdit(row)
    {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(id) {
      console.log(id)
      request.delete("/user/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()  // 删除之后重新加载表格的数据
      })
    },
    handleSizeChange()
    {

    },
    handleCurrentChange()
    {

    },

  }
}

</script>